# include <stdlib.h>
# include <stdio.h>
# include <string.h>
# include "Header.h"
# include "CreateVFS.h"

struct VFS *create_block (char *label) {
	struct VFS * temp;
	int i, l;
	FILE *fp;
	temp = (struct VFS *) malloc (sizeof(struct VFS));
	strcpy (temp -> label, label);
	for (i = 0; i < MAX_FD_SIZE; i++) {
		temp -> freeList[i] = 0;
		//temp -> fd[i] = (struct FileDescriptor *) malloc (sizeof(struct FileDescriptor));
		for (l = 0; l < 50; l++) 
			temp -> fd[i].fileName[l] = '\0';
		for (l = 0; l < 5; l++) 
			temp -> fd[i].fileType[l] = '\0';
		for (l = 0; l < 100; l++) 
			temp -> fd[i].filePath[l] = '\0';
		for (l = 0; l < 1024; l++) 
			temp -> bl[i].data[l] = '\0';
		temp -> fd[i].size = 0;
	}
	return temp;
}

void splitFileNames (struct VFS *header) {
	int i, j, k = 0, l;
	char tempPath[100],tempFN[50],tempEx[5];
	for (i = 0; i < MAX_FD_SIZE; i++) {
		if(header -> freeList[i]) {
			for (l = 0; l < 50; l++) {
				tempFN[l] = '\0';
				header -> fd[i].fileName[l] = '\0';
			}
			for (l = 0; l < 5; l++) {
				tempEx[l] = '\0';
				header -> fd[i].fileType[l] = '\0';
			}
			for (j = 0; header -> fd[i].filePath[j]; j++) {
				if ( header -> fd[i].filePath[j] == '/') {
					for (l = 0; l < 50 && tempFN[l]; l++)
						tempFN[l] = '\0';
					k = 0;
				}
				else {
					tempFN[k++] = header -> fd[i].filePath[j];
					//tempFN[k++] = tempPath[j];
				}
			}
			while (j < 100)
				header -> fd[i].filePath[j++] = '\0';
			j = 0, k = 0;
			while (tempFN[j++] != '.');
			while (tempFN[j + k]) {
				tempEx[k] = tempFN[j + k];
				k++;
			}
			j--;
			while (tempFN[j])
				tempFN[j++] = '\0';
			tempEx[k] = '\0';
			copyString(header -> fd[i].fileName, tempFN);
			copyString(header -> fd[i].fileType, tempEx);
		}
	}
}

char *loadFile (char *fileName) {	
	char *str, ch;
	//FILE *fp;
	//FILE *fp2;
	int i = 0;

	str = (char *) malloc(1050 * sizeof(char));
	FILE *fp = (FILE *)fopen(fileName,"r");
	for (i = 0; i < 1050; i++) {
		str[i] = '\0';
	}
	//fflush(fp);
	if (fp == NULL)
		return NULL;
	i = 0;
	while ((ch = fgetc(fp)) != EOF && i < 1045) {
		str[i++] = ch;
	}
	//fflush(fp);
	//fclose(fp);
	return str;
}

int checkFile (char *fileName) {
	FILE *fp = (FILE *)fopen(fileName,"r");

	if(fp != NULL) {
		fclose(fp);
		return 1;
	}
	else
		return 0;
}

int writeFile (char *fileName, char **data) {	
	char str[1024], ch;
	FILE *fp;
	int i = 0;

	fp = (FILE *)fopen(fileName,"w+");
	if (fp == NULL)
		return 1;
	while (*(data + i)) {
		fputs(*(data + i), fp);
		fputs("\n", fp);
		i++;
	}
	fflush(fp);
	//fclose(fp);
	return 0;
}

int writeFileLines (char *fileName, char **data) {	
	char str[1024], ch;
	FILE *fp;
	int i = 0;

	fp = (FILE *)fopen(fileName,"w+");
	if (fp == NULL)
		return 0;
	while (*(data + i)) {
		fputs(*(data + i), fp);
		fputs("\n", fp);
		i++;
	}
	fflush(fp);
	//fclose(fp);
	return i - 1;
}

int writeToFile (char *fileName, char *data) {	
	char str[1024], ch;
	FILE *fp;
	int i = 0;

	fp = (FILE *)fopen(fileName,"w+");
	//fflush(fp);
	if (fp == NULL)
		return 1;
	fputs(data, fp);

	fflush(fp);
	//fclose(fp);
	return 0;
}

int validateName (char *fileName) {
       int i = 0;
       while (*(fileName + i)) {
               if ((*(fileName + i) >= 65 && *(fileName + i) <= 90) || (*(fileName + i) >= 97 && *(fileName + i) <= 122) || (*(fileName + i) >= 48 && *(fileName + i) <= 57)) {
                       i++;
                       continue;
               }
               else
                       return 0;
               i++;
       }
       return 1;
}

int validateDirName (char *fileName) {
	int i = 0;
	while (*(fileName + i)) {
		if ((*(fileName + i) >= 65 && *(fileName + i) <= 90) || (*(fileName + i) >= 97 && *(fileName + i) <= 122) || (*(fileName + i) == '/') || (*(fileName + i) >= 48 && *(fileName + i) <= 57)) {
			i++;
			continue;
		}
		else
			return 0;
		i++;
	}
	return 1;
}

int validateFileName (char *fileName) {
	int i = 0;
	while (*(fileName + i)) {
		if (*(fileName + i) == '/') {
			return 0;
		}
		else
			i++;
	}
	return 1;
}

int getSize (char *data) {
	return stringLength(data);
}
 
int validateSize (int size) {
	/*if (size <= 0 || size > 1024)
		return 0;
	else
		return 1;
	*/
	if (size >= 1 && size <= 1024)
		return 1;
	else
		return 0;
}

void read_block (struct VFS *header, char *fileName) {	
	char str[100], *data;
	FILE *fp;
	int i = 0;

	fp = (FILE *) fopen(fileName,"r");
	while (fgets(str, 100, fp)) {
		header -> freeList[i] = 1;
   		copyString (header -> fd[i].filePath, str);
		//printf("Line: %d %s\n", i, str);
		data = loadFile (header -> fd[i].filePath);
		copyString(header -> bl[i].data, data);
		header -> fd[i].size = getSize(data);
		//puts(header -> bl[i].data);
		i++;
	}
	fclose(fp);

	splitFileNames(header);
}
